package com.mobileares.midp.widgets.client.list;

import com.google.gwt.event.dom.client.*;
import com.google.gwt.event.logical.shared.SelectionEvent;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;
import com.mobileares.midp.widgets.client.panel.EventFlowPanel;
import com.mobileares.midp.widgets.client.utils.ITextProvider;
import com.mobileares.midp.widgets.client.utils.IWidgetProvider;

/**
 * Created by IntelliJ IDEA.
 * User: Tom
 * Date: 2011-4-8
 * Time: 13:54:29
 * To change this template use File | Settings | File Templates.
 */
public class ListItem<T> extends Composite {
    protected EventFlowPanel container;
    private T userObject;
    protected ListWidget parent;
    protected Widget widget;

    public ListItem(T ob) {
        this.userObject = ob;
        container = new EventFlowPanel();
        container.setStyleName("list-item");
        initWidget(container);

    }

    public void setParent(ListWidget parent) {
        this.parent = parent;
        if (parent.getProvider() != null) {
            widget = (parent.getProvider().getWidget(userObject));
        } else
            widget = (new Label(userObject.toString()));
        container.add(widget);
        init();

    }
    public Widget getUserWidget(){
       return widget; 
    }


    public T getUserObject() {
        return userObject;
    }

    protected void init() {
        addEvent();
    }

    ClickHandler clickHandler = new ClickHandler() {
        public void onClick(ClickEvent event) {
            parent.setSelected(ListItem.this, true);
        }
    };

    private void addEvent() {
        container.addClickHandler(clickHandler);
        container.addMouseOverHandler(new MouseOverHandler() {
            public void onMouseOver(MouseOverEvent event) {
                container.addStyleDependentName("over");
            }
        });
        container.addMouseOutHandler(new MouseOutHandler() {
            public void onMouseOut(MouseOutEvent event) {
                container.removeStyleDependentName("over");
            }
        });
    }
}
